#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
class BigInt
{
private:
    string strNum;

public:
    BigInt(string number) : strNum(number)
    {
    }
    friend BigInt operator+(BigInt &lBigNum, BigInt &rBigNum);
    friend BigInt operator-(BigInt &lBigNum, BigInt &rBigNum);
    friend ostream &operator<<(ostream &out, BigInt &BigNum);
};

BigInt operator+(BigInt &lBigNum, BigInt &rBigNum)
{
    // 从末尾向前进行相加
    // 考虑进位情况
    int i = lBigNum.strNum.length() - 1;
    int j = lBigNum.strNum.length() - 1;
    string result = "";
    bool flag = false;
    while (i >= 0 && j >= 0)
    {
        int res = lBigNum.strNum[i] - '0' + rBigNum.strNum[j] - '0';
        if (flag)
        {
            // 说明上一步有进位的情况
            res += 1;
            flag = false;
        }

        if (res >= 10)
        {
            // 进位操作
            res = res % 10;
            flag = true;
        }
        result.push_back(res + '0');
        i--;
        j--;
    }

    if (i >= 0)
    {
        while (i >= 0)
        {
            int res = lBigNum.strNum[i] - '0';
            if (flag)
            {
                // 说明上一步有进位的情况
                res += 1;
                flag = false;
            }

            if (res >= 10)
            {
                // 进位操作
                res = res % 10;
                flag = true;
            }
            result.push_back(res + '0');
            i--;
        }
    }
    else if (j >= 0)
    {
        while (j >= 0)
        {
            int res = rBigNum.strNum[j] - '0';
            if (flag)
            {
                // 说明上一步有进位的情况
                res += 1;
                flag = false;
            }

            if (res >= 10)
            {
                // 进位操作
                res = res % 10;
                flag = true;
            }
            result.push_back(res + '0');
            j--;
        }
    }
    reverse(result.begin(), result.end());
    return BigInt(result);
}
BigInt operator-(BigInt &lBigNum, BigInt &rBigNum)
{
    // 减法一定是大减小，如果左边的小那就交换并且加负号
    string maxNum = lBigNum.strNum;
    string minNum = rBigNum.strNum;

    bool minus = false;
    // 是否借位
    bool flag = false;
    string result = "";
    if (maxNum.length() < minNum.length())
    {
        maxNum = rBigNum.strNum;
        minNum = lBigNum.strNum;
        minus = true;
    }
    else if (maxNum.length() == minNum.length())
    {
        if (maxNum < minNum)
        {
            maxNum = rBigNum.strNum;
            minNum = lBigNum.strNum;
            minus = true;
        }
        else if (maxNum == minNum)
        {
            // 说明左右数值完全相等
            return BigInt("0");
        }
    }

    int i = maxNum.length() - 1;
    int j = minNum.length() - 1;
    while (i >= 0 && j >= 0)
    {
        int res = (maxNum[i] - '0') - (minNum[j] - '0');

        if (flag)
        {
            // 上一步存在借位情况
            res -= 1;
            flag = false;
        }

        if (res < 0)
        {
            res += 10;
            flag = true;
        }
        result.push_back(res + '0');
        i--;
        j--;
    }
    if (i >= 0)
    {
        while (i >= 0)
        {
            int res = (maxNum[i] - '0');
            if (flag)
            {
                // 上一步存在借位情况
                res -= 1;
                flag = false;
            }

            if (res < 0)
            {
                res += 10;
                flag = true;
            }
            result.push_back(res + '0');
            i--;
        }
    }
    if(minus)
    {
        result.push_back('-');   
    }
    reverse(result.begin(),result.end());
    
    return BigInt(result);
}
ostream &operator<<(ostream &out, BigInt &bigNum)
{
    out << bigNum.strNum;
    return out;
}
int main()
{
    BigInt lBigNum("123456789123456");
    BigInt rBigNum("987654321654321");
    auto result = rBigNum - lBigNum;
    cout << result << endl;
}